package com.dcstudy.redis.controller.ioupgrade.bio.bioissuesolution;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author lichenyang
 * @create 2021-06-24 08:59
 * <p>
 * 使用多线程的方式解决了 bio存在的问题, 但是 使用多线程的方式会造成线程数量过多, 开销大 不建议使用
 */
public class RedisServerBIOMultiThread {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(6379);
        while (true) {
            System.out.println("-----111 等待连接 ");
            Socket socket = serverSocket.accept();
            //阻塞 1, 等待客户端连接
            System.out.println("-----222 成功连接 ");
            new Thread(() -> {
                try {
                    InputStream inputStream = socket.getInputStream();
                    int length = -1;
                    byte[] bytes = new byte[1024];
                    System.out.println("-----333 等待读取 ");
                    //阻塞 2, 等待客户端发送数据
                    while ((length = inputStream.read(bytes)) != -1) {
                        System.out.println("-----444 成功读取 " + new String(bytes, 0, length));
                        System.out.println("====================");
                        System.out.println();
                    }
                    inputStream.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }, Thread.currentThread().getName()).start();
            System.out.println(Thread.currentThread().getName());
        }
    }
}